﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using MyWeb.ServiceBus.Model;
using MyWeb.ServiceBus.Model.Models;
using MyWeb.ServiceBus.Services.SeedData;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace MyWeb.ServiceBus.Services.Db
{
    public class Context : DbContext
    {

        public Context(DbContextOptions<Context> options) : base(options)
        {
        }
        //protected override void OnModelCreating(ModelBuilder modelBuilder)
        //{
        //    base.OnModelCreating(modelBuilder);
        //    // 配置实体类之间的关系、主键等信息
        //    modelBuilder.Entity<Model.Models.Gateway>().Ignore(g=>g.Requsts);
            
        //    // 配置种子数据
        //    var seedData = new AppSystemSeedData().HasData();
        //    modelBuilder.Entity<AppSystem>().HasData(seedData.ToArray());



        //    // 获取当前程序集中的所有类型
        //    var types = Assembly.GetExecutingAssembly().GetTypes();

        //    // 找到所有实现 IEntitySeedData<T> 接口的类
        //    var seedDataTypes = types
        //        .Where(t => t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEntitySeedData<>)))
        //        .ToList();

        //    // 遍历这些类型并调用 HasData 方法来获取种子数据
        //    foreach (var seedDataType in seedDataTypes)
        //    {
        //        var entityType = seedDataType.GetInterfaces()
        //            .First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEntitySeedData<>))
        //            .GetGenericArguments()
        //            .First();

        //        var method = typeof(ModelBuilder).GetMethod("Entity", new Type[] { });
        //        var generic = method.MakeGenericMethod(entityType);
        //        dynamic? entityBuilder = generic.Invoke(modelBuilder, null);

        //        var instance = Activator.CreateInstance(seedDataType);
        //        var data = (IEnumerable<object>?)seedDataType.GetMethod("HasData").Invoke(instance, null);

        //        entityBuilder.HasData(data?.ToArray());
        //    }
        //    /// dotnet tool install --global dotnet-ef
        //    /// 初始化种子数据，执行以下两条命令
        //    /// dotnet ef migrations add SeedData
        //    /// dotnet ef database update

        //    // Other configurations

        //}
        /// <summary>
        /// 组织机构
        /// </summary>
        public DbSet<Organization> Organization { get; set; }
        /// <summary>
        /// 网关
        /// </summary>
        public DbSet<Model.Models.Gateway> Gateway { get; set; }
        /// <summary>
        /// 应用系统
        /// </summary>
        public DbSet<AppSystem> AppSystem { get; set; }
        /// <summary>
        /// 应用项目
        /// </summary>
        public DbSet<Application> Application { get; set; }
        /// <summary>
        /// 项目服务
        /// </summary>
        public DbSet<SysService> SysService { get; set; }
        /// <summary>
        /// 请求日志
        /// </summary>
        public DbSet<RequestLog> RequestLog { get; set; }
        /// <summary>
        /// 订阅服务
        /// </summary>
        public DbSet<SubServer> SubServer { get; set; }
        /// <summary>
        /// 接口
        /// </summary>
        public DbSet<Interface> Interface { get; set; }
        /// <summary>
        /// 接口文档
        /// </summary>
        public DbSet<InterfaceDoc> InterfaceDoc { get; set; }

    }
}